//
//  ZYStatusRetweetView.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/6/29.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit
import SnapKit

class ZYStatusRetweetView: UIView {
    
    //声明一个全局的转发微博底部的约束
    var retweetViewBottomConstraint:Constraint?
    
    
    //给转发微博赋值
    var status: ZYStatusModel?{
        didSet {
            
           // contentLabel.text = status?.retweeted_status?.text
            
            contentLabel.attributedText = status?.retweeted_status?.textAttr
            
            
            /*
            - 卸载当前转发微博底部约束
            - 如果status?.retweeted_status?.pic_urls.count > 0 代表有配图
            - 显示配图
            - 赋值
            - 更新约束
            - 反之没有配图
            - 隐藏配图
            - 更新约束
            */
            
            //卸载约束
            retweetViewBottomConstraint?.uninstall()
            //有配图的时候
            if status?.retweeted_status?.pic_urls?.count > 0  {
                //显示配图
                pictureView.hidden = false
                //进行赋值
                pictureView.pic_urls = status?.retweeted_status?.pic_urls
                //更新约束
                self.snp_makeConstraints { (make) -> Void in
                    //记录约束
                    retweetViewBottomConstraint =  make.bottom.equalTo(pictureView).offset(StatusMargin).constraint
                    
                }
            
            } else {
                
                pictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    //记录约束
                    retweetViewBottomConstraint =  make.bottom.equalTo(contentLabel).offset(StatusMargin).constraint
                    
                }
            
            }
            
            
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置转发微博的视图
    private func setupUI() {
        backgroundColor = RBG(247, green: 237, blue: 237)
        
        //添加到控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        
        //给contentLabel添加约束
        contentLabel .snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StatusMargin)
            make.top.equalTo(self).offset(StatusMargin)
            
        }
        
        //630 511
        //给配图设置约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusMargin)
            make.leading.equalTo(contentLabel)
            
            
        }
        
        //给自己添加约束
        self.snp_makeConstraints { (make) -> Void in
            //记录约束
           retweetViewBottomConstraint =  make.bottom.equalTo(pictureView).offset(StatusMargin).constraint

        }
        
       // contentLabel.text = "在MyViewController.m文件中，编译器也会自动的生成一个实例变量_myButton。那么在.m文件中可以直接的使用_myButton实例变量，也可以通过属性self.myButton.都是一样的。"
        
    
    }
    
    //懒加载控件
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 13, maxWidth: SCREENW - 2*StatusMargin)
    
    //630---转发微博的配图    5:11
    private lazy var pictureView: ZYPictureView = ZYPictureView()
    
    
    
    
}
